package org.example.config.sequence.configs.processor.factory;

import org.example.config.sequence.configs.beans.MyBeanDefinitionRegistryPostProcessorBean;
import org.example.config.sequence.inspect.Inspector;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

/**
 * 通过{@link Component @Component},{@link Bean @Bean}等注解声明方式加载，也可通过注册{@link BeanDefinition}方式加载
 * <p>
 * 本示例中通过{@link Component @Component}声明方式加载
 */
@Component
public class MyBeanDefinitionRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor {
    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry beanDefinitionRegistry) throws BeansException {
        Inspector.log(MyBeanDefinitionRegistryPostProcessor.class, "postProcessBeanDefinitionRegistry");
        BeanDefinition beanDefinition = BeanDefinitionBuilder
                .genericBeanDefinition(MyBeanDefinitionRegistryPostProcessorBean.class)
                .getBeanDefinition();
        beanDefinitionRegistry.registerBeanDefinition(MyBeanDefinitionRegistryPostProcessorBean.class.getSimpleName(), beanDefinition);
    }

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
        Inspector.log(MyBeanDefinitionRegistryPostProcessor.class, "postProcessBeanFactory");
    }
}
